// pages/bind/bind.js
const app = getApp();
import httpUtil from '../../utils/httpUtil'
Page({

  data: {
    countdown: 0,
    userName:'',
    password:'',
    mobile:'',
    vcode:'',
    cookie:''
  },

  loginbypassword(){
      var username = this.data.userName
      console.log(username)
      if(username == ''){
        wx.showToast({
          title: '请输入用户名',
          icon:'none'
        })
        return;
      }
      var passowrd = this.data.password
      if(passowrd == ''){
        wx.showToast({
          title: '请输入密码',
          icon:'none'
        })
        return;
      }
      console.log('用户名为'+username+'密码为'+passowrd)

      httpUtil.getRequest('/api/store/addBypassword',{
        'username':username,
        'password':passowrd
      }).then(res =>{
          console.log(res)
          wx.showToast({
            title: '登录成功',
          })
        wx.reLaunch({
            url: '/pages/index/index'
          })
      })
  } ,

  loginbycode(){
    var mobile = this.data.mobile
    if(mobile == ''){
      wx.showToast({
        title: '请输入手机号',
        icon:'none'
      })
      return;
    }
    var vcode = this.data.vcode
    if(vcode == ''){
      wx.showToast({
        title: '请输入验证码',
        icon:'none'
      })
      return;
    }
    console.log('手机号为'+mobile+'验证码码为'+vcode)
    httpUtil.getRequest('/api/store/loginByCode',{
      'mobile':mobile,
      'code':vcode
    }).then(res =>{
        wx.showToast({
            title: '登录成功',
          })
        wx.reLaunch({
            url: '/pages/index/index'
          })
    })
  },

  loginbycookie(){
     var cookie =  this.data.cookie
     if(cookie == ''){
      wx.showToast({
        title: '请输入cookie',
        icon:'none'
      })
      return;
    }
     console.log('cookie为'+cookie)
     httpUtil.getRequest('/api/store/loginByCookie',{
       'cookie':cookie
     }).then(res =>{
        wx.showToast({
            title: '登录成功',
          })
        wx.reLaunch({
            url: '/pages/index/index'
          })
     })
  },

  usernameInput:function(e){

    this.setData({
      userName: e.detail.value
    })
    console.log(this.data.userName)
  },
  passwordInput(e){
    this.setData({
      password: e.detail.value
    })
    console.log(this.data.password)
  },
  mobileInput(e){
    this.setData({
      mobile: e.detail.value
    })
    console.log(this.data.mobile)
  },
  vcodeInput(e){
    this.setData({
      vcode: e.detail.value
    })
    console.log(this.data.vcode)
  },
  cookieInput(e){
    this.setData({
      cookie: e.detail.value
    })
    console.log(this.data.cookie)
  },
  sendCode() {
    var mobile = this.data.mobile
    if(mobile == ''){
      wx.showToast({
        title: '请输入手机号',
        icon:'none'
      })
      return;
    }
    console.log('手机号为'+mobile)
    httpUtil.getRequest('/api/store/sendCode',{
      'mobile':mobile
    }).then(res => {
      this.setData({
        countdown: 60
      });
      const timer = setInterval(() => {
        if (this.data.countdown > 0) {
          this.setData({
            countdown: this.data.countdown - 1
          });
        } else {
          clearInterval(timer);
        }
      }, 1000);
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})